import inspect
from pydantic import conint

from common.base.base_api import BaseApi
from services.services_module.schemas.configs import LteConfig, Telemetry, GsmConfig, ModuleType
from settings import settings


class ServicesModuleApi(BaseApi):
    __slots__ = []

    def __init__(self):
        super(ServicesModuleApi, self).__init__(request_queue_uuid=settings.SERVICES_MODULE_REQUEST_QUEUE)

    async def set_lte_config(self, index: conint(ge=0, lt=settings.SERVICES_MODULE_NUMBER), config: LteConfig
                       ) -> bool | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index, 'config': config}
        )

    async def set_gsm_config(self, index: conint(ge=0, lt=settings.SERVICES_MODULE_NUMBER), config: GsmConfig
                       ) -> bool | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index, 'config': config}
        )

    async def get_all_lte_configs(self) -> list[LteConfig] | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: [LteConfig(**cfg) for cfg in result]
        )

    async def get_lte_config(self, index: conint(ge=0, lt=settings.SERVICES_MODULE_NUMBER)
                       ) -> LteConfig | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index},
            handle_result=lambda result: LteConfig(**result)
        )

    async def get_all_gsm_configs(self) -> list | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: [GsmConfig(**cfg) for cfg in result]
        )

    async def get_gsm_config(self, index: conint(ge=0, lt=settings.SERVICES_MODULE_NUMBER)
                       ) -> GsmConfig | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index},
            handle_result=lambda result: GsmConfig(**result)
        )

    async def get_all_module_types(self) -> list[ModuleType] | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def get_module_type(self, index: conint(ge=0, lt=settings.SERVICES_MODULE_NUMBER)) -> str | None:
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'index': index})

    async def get_all_telemetry(self) -> list[Telemetry] | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            handle_result=lambda result: [Telemetry(**cfg) for cfg in result]
        )

    async def get_telemetry(self, index: conint(ge=0, lt=settings.SERVICES_MODULE_NUMBER)
                      ) -> Telemetry | None:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {'index': index},
            handle_result=lambda result: Telemetry(**result)
        )
